/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: MyAuthenticationFailureHandler
 * Author:   吴建森
 * Date:     2020/12/6 17:44
 * Description:
 * History:
 * <author>吴建森      <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.sen.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sen.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author 吴建森
 * @create 2020/12/6
 * @since 1.0.0
 */
@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Autowired
    ObjectMapper objectMapper;
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = response.getWriter();

        String name = objectMapper.writeValueAsString(R.error(508,"认证失败："+e.getMessage()));
        writer.print(name);
        writer.flush();
        writer.close();
    }
}
